package com.itheima.ui;

import cn.hutool.core.io.FileUtil;
import com.itheima.user.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

//创建 RegisterJFrame 类 继承 JFrame 类
public class RegisterJFrame extends JFrame {
    ArrayList<User> userList = new ArrayList<>();

    //用户名
    JTextField jTextField = new JTextField();

    //密码
    JPasswordField jPasswordField = new JPasswordField();
    //二次密码
    JPasswordField rejPasswordField = new JPasswordField();

    //注册按钮
    JButton registerBtn = new JButton(new ImageIcon("image\\register\\注册按钮.png"));

    //重置按钮
    JButton resetBtn = new JButton(new ImageIcon("image\\register\\重置按钮.png"));


    //创建空参构造并初始化界面窗口
    public RegisterJFrame(ArrayList<User> userList) {
        this.userList = userList;
        initJFrame();
        initJFrameView();
        //设置窗口显示
        this.setVisible(true);

    }

    //加载注册界面视图
    private void initJFrameView() {

        resetBtn.setBounds(310, 370, 128, 47);
        resetBtn.setContentAreaFilled(false);//取消蓝色背景
        resetBtn.setBorderPainted(false);//取消边框
        this.getContentPane().add(resetBtn);


        //绑定点击重置按钮
        resetBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jTextField.setText("");
                jPasswordField.setText("");
                rejPasswordField.setText("");
            }
        });

        //绑定注册按钮鼠标按下抬起
        resetBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                resetBtn.setIcon(new ImageIcon("image\\register\\重置按下.png"));
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                resetBtn.setIcon(new ImageIcon("image\\register\\重置按钮.png"));
            }
        });

        //注册按钮
        registerBtn.setBounds(160, 370, 128, 47);
        registerBtn.setContentAreaFilled(false);//取消蓝色背景
        registerBtn.setBorderPainted(false);//取消边框
        this.getContentPane().add(registerBtn);

        //绑定点击注册按钮
        registerBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //1.判断用户名、密码是否为空
                if (jTextField.getText().isEmpty()) {
                    showDialog("用户名不能为空");
                    return;
                }
                if (!jTextField.getText().matches("[a-zA-Z0-9]{4,16}")) {
                    showDialog("用户名不符合规则");
                    return;
                }
                if (jPasswordField.getPassword().length == 0 || rejPasswordField.getPassword().length == 0) {
                    showDialog("密码不能为空");
                    return;
                }
                //2.判断用户名、密码是否符合规则

                if (!new String(jPasswordField.getPassword()).matches("^\\S*(?=\\S{6,})(?=\\S*\\d)(?=\\S*[a-z])\\S*$")) {
                    showDialog("密码不符合规则,请输入包含小写字母、数字且六位以上的密码");
                    return;
                }
                //3.判断两次的密码是否一致
                if (!new String(jPasswordField.getPassword()).equals(new String(rejPasswordField.getPassword()))) {
                    showDialog("两次密码不一致");
                    return;
                }
                //4.判断用户名是否已经存在
                User user = new User(jTextField.getText(), new String(jPasswordField.getPassword()));
                boolean isExist = isContains(userList,user.getUsername());
                if (isExist) {
                    showDialog("用户民已存在，请重新输入");
                    return;
                }
                //5.把用户信息加到集合中
                userList.add(user);
                //6.把用户信息写入文件
                ArrayList<String> tempList = new ArrayList<>();
                for (User ur : userList) {
                    tempList.add(ur.toString());
                }

                FileUtil.writeLines(tempList, "E:\\Java\\jigsaw-puzzle\\user_list.txt", "UTF-8");

                //7.提示注册成功
                showDialog("恭喜你，注册成功");
                setVisible(false);
                new LoginJFrame();

            }
        });

        //绑定注册按钮鼠标按下抬起
        registerBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                registerBtn.setIcon(new ImageIcon("image\\register\\注册按下.png"));
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                registerBtn.setIcon(new ImageIcon("image\\register\\注册按钮.png"));
            }
        });


        //再次密码
        JLabel rePasswordLabel = new JLabel(new ImageIcon("image\\register\\再次输入密码.png"));
        rePasswordLabel.setBounds(100, 290, 97, 40);
        this.getContentPane().add(rePasswordLabel);


        rejPasswordField.setFont(new Font("", Font.ROMAN_BASELINE, 20));
        rejPasswordField.setBounds(200, 290, 200, 40);

        this.getContentPane().add(rejPasswordField);

        //密码
        JLabel passwordLabel = new JLabel(new ImageIcon("image\\register\\注册密码.png"));
        passwordLabel.setBounds(110, 240, 87, 40);
        this.getContentPane().add(passwordLabel);


        jPasswordField.setFont(new Font("", Font.ROMAN_BASELINE, 20));
        jPasswordField.setBounds(200, 240, 200, 40);

        this.getContentPane().add(jPasswordField);


        //用户名
        JLabel usernameLabel = new JLabel(new ImageIcon("image\\register\\注册用户名.png"));
        usernameLabel.setBounds(110, 190, 87, 40);
        this.getContentPane().add(usernameLabel);

        jTextField.setBounds(200, 190, 200, 40);
        jTextField.setFont(new Font("", Font.ROMAN_BASELINE, 20));
        this.getContentPane().add(jTextField);

        //设置背景
        JLabel bgImage = new JLabel(new ImageIcon("image\\register\\background.png"));
        bgImage.setBounds(60, 50, 470, 390);
        this.getContentPane().add(bgImage);
    }


    //初始化注册窗口
    private void initJFrame() {
        //设置窗口大小
        this.setSize(600, 550);
        //设置窗口标题
        this.setTitle("拼图游戏 注册");
        //设置窗口置顶层
        this.setAlwaysOnTop(true);
        //设置窗口居中
        this.setLocationRelativeTo(null);
        //设置窗口关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        this.setLayout(null);
    }

    private void showDialog(String message) {
        JOptionPane optionPane = new JOptionPane();
        optionPane.setMessage(message);
        JDialog dialog = optionPane.createDialog("提示");
        dialog.setAlwaysOnTop(true);
        dialog.setVisible(true);
    }


    private boolean isContains(ArrayList<User> list, String name) {
        for (User user : list) {
            if (user.getUsername().equals(name)) {
                return true;
            }
        }
        return false;
    }

}
